home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
ppc.zip
/
PPC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-09-17
|
16KB
|
559 lines
/* PPC.C - Pretty Printer for C
Created from CB on SIMTEL20 by Richard Conn
CB was heavily modified to create PPC */
#include <stdio.h>
#define TITLE "Pretty Printer for C, Beta Test Version 1.0"
#define MAXBRAC 20 /* maximum number of open braces allowed */
#define IFLEVEL 20 /* maximum number of nested IFs allowed */
int slevel[IFLEVEL];
int spflg[MAXBRAC][IFLEVEL];
int sind [MAXBRAC][IFLEVEL];
int siflev[IFLEVEL];
int sifflg[IFLEVEL];
int clevel = 0; /* Number of open braces */
int iflev = 0; /* Number of active IFs */
int ifflg = -1; /* Indicates if we have an active IF */
int level = 0;
int ind[IFLEVEL]; /* Indentation amount for a particular IF */
int pflg[IFLEVEL]; /* Number of characters to indent for a particular IF */
int eflg = 0; /* We are in an ELSE? */
int paren = 0; /* Number of open parentheses */
int tabcount = 4; /* Number of characters to indent */
char lchar;
char pchar;
int aflg = 0;
int stabs[MAXBRAC][IFLEVEL];
int qflg = 0;
/* The following are reserved words used for special processing */
char *wstr[] = {
"typedef",
"struct",
"union",
"int",
"char",
"unsigned",
"long",
"auto",
NULL
};
char *wrsvwds[] = {
"int",
"char",
"unsigned",
"long",
"auto",
NULL
};
char *wif[] = {
"if",
NULL
};
char *welse[] = {
"else",
NULL
};
char *wfor[] = {
"for",
NULL
};
char *wds[] = {
"case",
"default",
NULL
};
/* J is the index for the next character in the STRING buffer
STRING is the buffer in which the output line is built */
int j = 0;
char string[400];
char cc;
int sflg = 1;
int bflg = 0;
int peek = -1;
int tabs = 0;
int lastchar = ' ';
int cin;
/* Input and output buffers */
FILE *inpbuf;
FILE *outbuf;
main(argc, argv)
int argc;
char *argv[];
{
char bakfil[100]; /* name of current backup file */
int filno; /* number of current file */
int cont; /* local continuation flag */
int ct; /* count of local open parens */
int eflg; /* indicates we are in an ELSE */
int i;
if (argc == 1) {
printf ("%s\n", TITLE);
printf (" Syntax: ppc [-i#] filename.ext [filename.ext ...]\n");
printf (" Options:\n");
printf (" -i# Set indentation level for files\n");
exit (0);
}
/* Zero indentation indicator and indent character count buffers */
for (i=0; i<IFLEVEL; i++) {
ind[i] = 0;
pflg[i] = 0;
}
/* Main Loop */
for (filno=1; argv[filno]!=NULL; filno++) {
/* Process next file (if not an option) */
if (*argv[filno] != '-') {
mkext (bakfil, argv[filno], "bak");
unlink (bakfil);
rename (argv[filno], bakfil);
if ((inpbuf = fopen (bakfil, "r")) == NULL) {
perror (bakfil);
exit (1);
}
if ((outbuf = fopen (argv[filno], "w")) == NULL) {
perror (argv[filno]);
exit (1);
}
fprintf (stderr, " Pretty Printing %s\n", argv[filno]);
while ( (cin = getchr()) != EOF) {
switch(cin) {
case ' ':
case '\t':
if (lookup(welse) == 1) {
gotelse ();
if (sflg == 0 || j > 0) string[j++] =cin;
putz ();
sflg = 0;
break;
}
if (sflg == 0 || j > 0) string[j++] =cin;
break;
case '\n':
if ((eflg = lookup(welse)) == 1) gotelse();
putz();
fprintf (outbuf,"\n");
sflg = 1;
if (eflg == 1) {
pflg[level]++;
tabs++;
}
else
if (pchar == lchar)
aflg = 1;
break;
case '{':
if (lookup(welse) == 1) gotelse();
siflev[clevel]= iflev;
sifflg[clevel]= ifflg;
iflev = ifflg = 0;
clevel++;
if (sflg == 1 && pflg[level] != 0) {
pflg[level]--;
tabs--;
}
string[j++] =cin;
putz ();
getnl ();
putz ();
fprintf (outbuf,"\n");
tabs++;
sflg = 1;
if (pflg[level] > 0) {
ind[level] = 1;
level++;
slevel[level] = clevel;
}
break;
case '}':
clevel--;
if ((iflev = siflev[clevel]-1) < 0) iflev = 0;
ifflg = sifflg[clevel];
putz ();
tabs--;
ptabs ();
if ((peek = getchr()) == ';') {
fprintf (outbuf, "%c;", cin);
peek = -1;
}
else fprintf (outbuf,"%c", cin);
getnl ();
putz ();
fprintf (outbuf,"\n");
sflg = 1;
if (clevel < slevel[level]) if (level > 0) level--;
if (ind[level] != 0) {
tabs -= pflg[level];
pflg[level] =0;
ind[level] = 0;
}
break;
case '"':
case '\'':
string[j++] =cin;
while ((cc = getc(inpbuf)) !=cin) {
string[j++] =cc;
if (cc == '\\') string[j++] = getc(inpbuf);
if (cc == '\n') {
putz();
sflg = 1;
}
}
string[j++] =cc;
if (getnl() ==1) {
lchar = cc;
peek = '\n';
}
break;
case ';':
string[j++] =cin;
putz ();
if (pflg[level] > 0 && ind[level] == 0) {
tabs -= pflg[level];
pflg[level] = 0;
}
getnl ();
putz ();
fprintf (outbuf,"\n");
sflg = 1;
if (iflev > 0)
if (ifflg == 1) {
iflev--;
ifflg = 0;
}
else iflev = 0;
break;
case '\\':
string[j++] =cin;
string[j++] =getchr();
break;
case '?':
qflg = 1;
string[j++] =cin;
break;
case ':':
string[j++] =cin;
if (qflg == 1) {
qflg = 0;
break;
}
if (lookup(wrsvwds) == 1) break;
if (lookup(wds)== 0) {
sflg = 0;
putz ();
}
else {
tabs--;
putz ();
tabs++;
}
if ((peek = getchr()) == ';') {
fprintf (outbuf, ";");
peek = -1;
}